package com.sukaiyi.giteeideaplugin.mainwindow.repodetailtabbedpane;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.json.JSONObject;
import com.intellij.util.ui.AnimatedIcon;
import com.sukaiyi.giteeideaplugin.component.JFXStaticWebView;
import com.sukaiyi.giteeideaplugin.utils.FileTypeUtils;
import com.sukaiyi.giteeideaplugin.utils.IdeaUIUtils;
import com.sukaiyi.giteeideaplugin.utils.NotificationUtils;
import com.sukaiyi.giteejavaapi.common.Gitee;
import com.sukaiyi.giteejavaapi.common.Params;
import com.sukaiyi.giteejavaapi.http.model.HttpResponse;
import javafx.application.Platform;
import javafx.scene.web.WebEngine;

import javax.swing.*;

/**
 * @author sukaiyi
 * @date 2020/07/16
 */
public class ReadmeTabbedPane implements RepoDetailPane {

    private final JSONObject repo;

    private JPanel content;
    private AnimatedIcon animatedIcon;
    private final JFXStaticWebView webViewContainer;

    public ReadmeTabbedPane(JSONObject repo) {
        this.repo = repo;
        webViewContainer = JFXStaticWebView.getInstance();
        refresh();
    }

    private void refresh() {
        Params params = Params.create()
                .set("owner", repo.getByPath("namespace.path", String.class))
                .set("repo", repo.getStr("path"))
                .set("ref", repo.getStr("default_branch"));
        Gitee.getInstance().invoke("getV5ReposOwnerRepoReadme", params)
                .thenApply(HttpResponse::asJSONObject)
                .thenApply(e -> e.getStr("content"))
                .thenApply(Base64::decodeStr)
                .thenApply(markdown -> {
                    String template = new ClassPathResource("/template/markdown.html", ReadmeTabbedPane.class.getClassLoader()).readUtf8Str();
                    String showdown = new ClassPathResource("/template/showdown/1.9.1/showdown.min.js", ReadmeTabbedPane.class.getClassLoader()).readUtf8Str();
                    markdown = markdown.replace("'", "\"").replace("\r\n", "\n").replace("\n", "\\n");
                    return String.format(template, showdown, markdown);
                })
                .whenComplete((res, ex) -> {
                    if (ex != null) {
                        NotificationUtils.notify(ex.getMessage());
                    } else {
                        Platform.runLater(() -> {
                            WebEngine webEngine = webViewContainer.getWebEngine();
                            webEngine.loadContent(res);
                        });
                        SwingUtilities.invokeLater(() -> {
                            content.remove(animatedIcon);
                            content.add(webViewContainer);
                        });
                    }
                });
    }

    public void createUIComponents() {
        animatedIcon = IdeaUIUtils.createBigProgressIcon();
        animatedIcon.resume();
    }

    @Override
    public String getTitle() {
        return "README.md";
    }

    @Override
    public Icon getIcon() {
        return FileTypeUtils.getIconForSuffix("md");
    }

    @Override
    public int getIndex() {
        return -1;
    }
}
